#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@author:Gwong Zaak Liu
@interpreter version:python 3.7.3
@file:  ChecksumIfValuable.py
@time:  2020/08
"""

from fake_useragent import UserAgent
import asyncio
from pyppeteer import launch

from pyppeteer.network_manager import Request

Url = "https://lhttp.qingting.fm/live/386/64k.mp3"
List = []


def Checksum():
    async def modify_url(request: Request):
        if request.url == "https://www.baidu.com/":
            await request.continue_({"url": "https://www.baidu.com/s?wd=ip&ie=utf-8"})
        else:
            await request.continue_()

    async def interception_test():
        launch_args = {
            "headless": False,
            # "executablePath": r"~/Library/Application
            # Support/pyppeteer/local-chromium/588429/chrome-mac/Chromium.app",
            "args": [
                "--start-maximized",
                "--no-sandbox",
                "--disable-infobars",
                "--ignore-certificate-errors",
                "--log-level=3",
                "--enable-extensions",
                # 设置窗口大小为高100px宽300px
                "--window-size=100,300",
                "".join(["--user-agent=", UserAgent(verify_ssl=False).random]),
            ],
        }
        # 启动浏览器
        browser = await launch(**launch_args)
        # 新建标签页
        page = await browser.newPage()
        # 设置页面打开超时时间
        page.setDefaultNavigationTimeout(10 * 1000)
        # 设置窗口大小
        await page.setViewport({"width": 400, "height": 200})

        # 启用拦截器
        await page.setRequestInterception(True)

        # 设置拦截器
        # 修改请求的url
        if 1:
            page.on("request", modify_url)
            global Url
            res = await page.goto(Url)
            mess = "%s \n 状态码：%s" % (Url, res.status)
            print(mess)
            if res.status == 200:
                List.append(mess)
            print(List)
        await asyncio.sleep(10)

        # 关闭标签页
        await page.close()
        # 关闭浏览器
        # await browser.close()
        return

    if __name__ == "__main__":
        loop2 = asyncio.get_event_loop()
        loop2.run_until_complete(interception_test())


Checksum()
